#include "Demo.h"

#include "util.h"

Demo* Demo::_this = NULL;

Demo::Demo(LPCSTR title)
	:EngineD3D(title)
	,_camAngle(0.0f),_camRadius(10.0f),_camHeight(0.0f)
	,_fGeoAngle(0.0f)
{
	_this = this;

	v3ViewTarget = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	v3ViewUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
	D3DXMatrixPerspectiveFovLH(&mProj, D3DX_PI/4.0f, (FLOAT)SCREEN_WIDTH/(FLOAT)SCREEN_HEIGHT, 1.0f, 500.0f);

	_SetCam();

	ZeroMemory(&_geoRhom1,sizeof(Geometry));
	ZeroMemory(&_geoRhom2,sizeof(Geometry));

	class LogID : public MouseTrack
	{
	private:
		DWORD _id;
	public:
		LogID(DWORD id)
			:_id(id)
		{

		}
		void OnLeftButtonDown(WORD x, WORD y)
		{
			printf("Object: %d\n",_id);
		}
	};
	_geoRhom1.mt = new LogID(1);
	_geoRhom2.mt = new LogID(2);

	_geoRhom1.v4Mate = D3DXVECTOR4(0.5f,0.8f,1.0f,10.0f);
	_geoRhom2.v4Mate = D3DXVECTOR4(0.3f,0.5f,10.0f,100.0f);

	_SetRhoms();

	sun.v3Color = D3DXVECTOR3(1.0f,1.0f,1.0f);
	sun.v3Dir = D3DXVECTOR3( 0.0f,-3.0f, 0.0f);
	
	for(int i=0; i<3; i++)
	{
		_litPoints[i].v3Atte = D3DXVECTOR3(0.0f,0.0f,1.0f);
	}

	_litPoints[0].v3Color = D3DXVECTOR3(5.0f,10.0f,5.0f);
	_litPoints[1].v3Color = D3DXVECTOR3(10.0f,5.0f,5.0f);
	_litPoints[2].v3Color = D3DXVECTOR3(5.0f,5.0f,10.0f);

	_litPoints[0].v3Pos = D3DXVECTOR3( 0.0f, 3.0f, 0.0f);
	_litPoints[1].v3Pos = D3DXVECTOR3( 0.0f, 3.0f,-3.0f);
	_litPoints[2].v3Pos = D3DXVECTOR3( 0.0f, 3.0f, 3.0f);

	for(int i=0; i<3; i++){
		lights.push_front(&_litPoints[i]);
		_itlits[i] = lights.begin();
	}

	for(int i=0; i<256; i++)
	{
		_hold[i] = FALSE;
	}
}

void Demo::_SetCam()
{
	v3ViewEye = D3DXVECTOR3(_camRadius*cosf(_camAngle),_camHeight,_camRadius*sinf(_camAngle));
}

void Demo::_SetRhoms()
{
	D3DXMATRIX mTrans;
	D3DXMATRIX mRotate;
	D3DXMatrixTranslation(&mTrans, 3.0f, 0.0f, 0.0f);
	D3DXMatrixRotationY(&mRotate,_fGeoAngle);
	_geoRhom1.mWorld = mRotate*mTrans;
	D3DXMatrixTranslation(&mTrans,-3.0f, 0.0f, 0.0f);
	D3DXMatrixRotationY(&mRotate,_fGeoAngle*2.0f);
	_geoRhom2.mWorld = mRotate*mTrans;
}

Demo::_Rolling::_Rolling()
	:Timer(50)
{

}

void Demo::_Rolling::OnTick()
{
	_this->_fGeoAngle += 1.0f/600.0f*D3DX_PI;
	_this->_SetRhoms();
}

void Demo::OnLoad()
{
	HR(D3DUCreateRhomb(d3ddev,&_geoRhom1.d3dvb,&_geoRhom1.d3dib));
	HR(D3DUCreateRhomb(d3ddev,&_geoRhom2.d3dvb,&_geoRhom2.d3dib));
	HR(D3DXCreateTextureFromFile(d3ddev,"tex_1.bmp",&_geoRhom1.d3dtex));
	HR(D3DXCreateTextureFromFile(d3ddev,"tex.png",&_geoRhom2.d3dtex));

	//world rotation
	new _Rolling();
	//add to pool
	geos.push_front(&_geoRhom1);
	geos.push_front(&_geoRhom2);
}

Demo::_HoldCamera::_HoldCamera(BYTE code)
	:Timer(50),_code(code)
{

}

void Demo::_HoldCamera::OnTick()
{
	switch(_code){
	case 'A':
		Demo::_this->_camAngle += D3DX_PI/18.0f;
		break;
	case 'D':
		Demo::_this->_camAngle -= D3DX_PI/18.0f;
		break;
	case 'W':
		if(Demo::_this->_camHeight<55.0f)
		{
			Demo::_this->_camHeight += 5.0f;
		}
		break;
	case 'S':
		if(Demo::_this->_camHeight>-45.0f)
		{
			Demo::_this->_camHeight -= 5.0f;
		}
		break;
	case 'Q':
		if(Demo::_this->_camRadius>1.0f)
		{
			Demo::_this->_camRadius *= 0.8f;
		}
		break;
	case 'E':
		if(Demo::_this->_camRadius<500.0f)
		{
			Demo::_this->_camRadius *= 1.25f;
		}
		break;
	default:
		return;
	}
	Demo::_this->_SetCam();
}

void Demo::OnKeyDown(BYTE code)
{
	int idx = code-'1';
	switch(code){
	case 'A':
	case 'D':
	case 'W':
	case 'S':
	case 'Q':
	case 'E':
		_hold[code] = new _HoldCamera(code);
		break;
	case '1':
	case '2':
	case '3':
		if(_itlits[idx]!=lights.end())
		{
			lights.erase(_itlits[idx]);
			_itlits[idx] = lights.end();
		}
		else
		{
			lights.push_front(&_litPoints[idx]);
			_itlits[idx] = lights.begin();
		}
		break;
	default:
		return;
	}
}

void Demo::OnKeyUp(BYTE code)
{
	if(_hold[code])
	{
		delete _hold[code];
	}
	_hold[code] = NULL;
}
